import {
  getCustomerTable,
  editCustomerRecord
} from '../../api/customer'
import {
  INTERESTED_LIST,
  INTERESTED_CURRENT_ROW,
  INTERESTED_DATA_COUNT,
  INTERESTED_HISTORY_DATA,
  INTERESTED_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    interestedList: [],
    interestedDataCount: null,
    interestedPageSize: 20,
    interestedHistoryData: [],
    row: {
      uid: '',
      ctime: '',
      uname: '',
      tel: '',
      coludname: '',
      coludtel: '',
      mname: '',
      appointime: '',
      state: '',
      comdate: '',
      tname: '',
      gid: '',
      gname: '',
      orderdate: '',
      cprice: '',
      commission: '',
      state0: '',
      ttype: '',
      remarks: '',
      perrate: '',
      permoney: '',
      budget: '',
      difftime: '',
      lostdate: '',
      sex: '',
      age: '',
      want: '',
      pname: '',
      puid: ''
    }
  },
  getters: {
    interestedList: state => state.interestedList
  },
  mutations: {
    [INTERESTED_LIST] (state, data) {
      state.interestedList = data
    },
    [INTERESTED_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [INTERESTED_DATA_COUNT] (state, data) {
      state.interestedDataCount = data
    },
    [INTERESTED_HISTORY_DATA] (state, data) {
      state.interestedHistoryData = data
    },
    [INTERESTED_PAGE_SIZE] (state, data) {
      state.interestedPageSize = data
    }
  },
  actions: {
    async getInterestedList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getCustomerTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        if (item.state === 0 && item.state0 === 0) {
          newData = [...newData, {
            id: item.id,
            uid: item.uid,
            ctime: item.ctime,
            uname: item.uname,
            tel: item.tel,
            coludname: item.coludname,
            coludtel: item.coludtel,
            mname: item.mname,
            pname: item.pname,
            puid: item.puid,
            appointime: item.appointime,
            state: item.state.toString(),
            comdate: item.comdate,
            tname: item.tname,
            gid: item.gid,
            gname: item.gname,
            orderdate: item.orderdate,
            cprice: item.cprice,
            commission: item.commission,
            state0: item.state0,
            ttype: item.ttype ? item.ttype.toString() : '',
            remarks: item.remarks,
            perrate: item.perrate,
            permoney: item.permoney,
            budget: item.budget,
            difftime: item.difftime,
            lostdate: item.lostdate,
            sex: item.sex,
            age: item.age || item.age === 0 ? item.age.toString() : '',
            want: item.want || item.want === 0 ? item.want.toString() : ''
          }]
        }
      })
      commit(INTERESTED_LIST, newData)
      commit(INTERESTED_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.interested.interestedPageSize) {
        commit(INTERESTED_HISTORY_DATA, newData)
      } else {
        commit(INTERESTED_HISTORY_DATA, newData.slice(0, rootState.interested.interestedPageSize))
      }
      return resp
    },
    async editInterestedData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editCustomerRecord(data)
      return resp
    },
    async getInterestedCurrentRow ({ commit, rootState }, row) {
      commit(INTERESTED_CURRENT_ROW, row)
    },
    async changeInterestedPage ({ commit, rootState }, index) {
      let data = rootState.interested.interestedList
      let _start = (index - 1) * rootState.interested.interestedPageSize
      let _end = index * rootState.interested.interestedPageSize
      commit(INTERESTED_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeInterestedPageSize ({ commit, rootState }, value) {
      commit(INTERESTED_PAGE_SIZE, value)
    }
  }
}
